using CollabApp.Domain.Entities.Game;
using CollabApp.Domain.ValueObjects;

namespace CollabApp.Domain.Services
{
    /// <summary>
    /// 画线圈地游戏核心逻辑服务接口
    /// </summary>
    public interface ILineDrawingGameLogicService
    {
        /// <summary>
        /// 检查玩家移动是否合法
        /// </summary>
        /// <param name="playerId">玩家ID</param>
        /// <param name="newPosition">新位置</param>
        /// <param name="gameState">游戏状态</param>
        /// <returns>移动结果</returns>
        Task<MoveResult> ValidatePlayerMove(Guid playerId, Position newPosition, object gameState);

        /// <summary>
        /// 检查画线路径是否与其他玩家轨迹碰撞
        /// </summary>
        /// <param name="playerId">玩家ID</param>
        /// <param name="path">画线路径</param>
        /// <param name="gameState">游戏状态</param>
        /// <returns>碰撞检测结果</returns>
        Task<CollisionResult> CheckPathCollision(Guid playerId, List<Position> path, object gameState);

        /// <summary>
        /// 计算圈地完成后的领地面积
        /// </summary>
        /// <param name="playerId">玩家ID</param>
        /// <param name="closedPath">封闭路径</param>
        /// <param name="existingTerritories">现有领地</param>
        /// <returns>领地计算结果</returns>
        Task<TerritoryResult> CalculateTerritory(Guid playerId, List<Position> closedPath, List<Territory> existingTerritories);

        /// <summary>
        /// 处理玩家死亡逻辑
        /// </summary>
        /// <param name="playerId">死亡玩家ID</param>
        /// <param name="killerPlayerId">击杀玩家ID（可选）</param>
        /// <param name="deathReason">死亡原因</param>
        /// <returns>死亡处理结果</returns>
        Task<DeathResult> HandlePlayerDeath(Guid playerId, Guid? killerPlayerId, string deathReason);

        /// <summary>
        /// 计算玩家复活时间
        /// </summary>
        /// <param name="playerId">玩家ID</param>
        /// <param name="consecutiveDeaths">连续死亡次数</param>
        /// <returns>复活时间（毫秒）</returns>
        Task<int> CalculateRespawnTime(Guid playerId, int consecutiveDeaths);

        /// <summary>
        /// 生成道具
        /// </summary>
        /// <param name="gameArea">游戏区域</param>
        /// <param name="existingPowerUps">现有道具</param>
        /// <param name="territoryAreas">领地区域</param>
        /// <returns>新生成的道具</returns>
        Task<PowerUp?> GeneratePowerUp(object gameArea, List<PowerUp> existingPowerUps, List<Territory> territoryAreas);

        /// <summary>
        /// 应用道具效果
        /// </summary>
        /// <param name="playerId">玩家ID</param>
        /// <param name="powerUp">道具</param>
        /// <param name="gameState">游戏状态</param>
        /// <returns>道具效果结果</returns>
        Task<PowerUpResult> ApplyPowerUpEffect(Guid playerId, PowerUp powerUp, object gameState);

        /// <summary>
        /// 检查游戏结束条件
        /// </summary>
        /// <param name="gameState">游戏状态</param>
        /// <returns>是否结束及结束原因</returns>
        Task<GameEndResult> CheckGameEndConditions(object gameState);

        /// <summary>
        /// 计算最终排名和积分
        /// </summary>
        /// <param name="players">游戏玩家列表</param>
        /// <returns>排名结果</returns>
        Task<List<PlayerRankResult>> CalculateFinalRanking(List<GamePlayer> players);

        /// <summary>
        /// 检查特殊事件触发
        /// </summary>
        /// <param name="gameMode">游戏模式</param>
        /// <param name="gameProgress">游戏进度（0-1）</param>
        /// <returns>特殊事件</returns>
        Task<SpecialEvent?> CheckSpecialEventTrigger(string gameMode, double gameProgress);

        /// <summary>
        /// 应用动态平衡机制
        /// </summary>
        /// <param name="players">游戏玩家列表</param>
        /// <returns>平衡调整结果</returns>
        Task<BalanceResult> ApplyDynamicBalance(List<GamePlayer> players);
    }

    /// <summary>
    /// 移动结果
    /// </summary>
    public class MoveResult
    {
        public bool IsValid { get; set; }
        public string? ErrorMessage { get; set; }
        public double MovementSpeed { get; set; } = 1.0;
        public bool IsInEnemyTerritory { get; set; }
    }

    /// <summary>
    /// 碰撞检测结果
    /// </summary>
    public class CollisionResult
    {
        public bool HasCollision { get; set; }
        public Guid? CollidedWithPlayerId { get; set; }
        public Position? CollisionPoint { get; set; }
        public string? CollisionType { get; set; }
    }

    /// <summary>
    /// 领地计算结果
    /// </summary>
    public class TerritoryResult
    {
        public bool IsValid { get; set; }
        public double NewTerritoryArea { get; set; }
        public double TotalTerritoryArea { get; set; }
        public List<Territory> EngulfedTerritories { get; set; } = new();
        public double EngulfedArea { get; set; }
    }

    /// <summary>
    /// 死亡处理结果
    /// </summary>
    public class DeathResult
    {
        public bool ShouldRespawn { get; set; }
        public int RespawnTimeMs { get; set; }
        public double RemainingTerritoryArea { get; set; }
        public bool HasPenalty { get; set; }
    }

    /// <summary>
    /// 道具效果结果
    /// </summary>
    public class PowerUpResult
    {
        public bool Applied { get; set; }
        public string? Effect { get; set; }
        public int DurationMs { get; set; }
        public Dictionary<string, object>? Parameters { get; set; }
    }

    /// <summary>
    /// 游戏结束结果
    /// </summary>
    public class GameEndResult
    {
        public bool ShouldEnd { get; set; }
        public string? Reason { get; set; }
        public Guid? WinnerId { get; set; }
    }

    /// <summary>
    /// 玩家排名结果
    /// </summary>
    public class PlayerRankResult
    {
        public Guid PlayerId { get; set; }
        public int Rank { get; set; }
        public double TerritoryArea { get; set; }
        public int ScoreChange { get; set; }
        public Dictionary<string, object>? Stats { get; set; }
    }

    /// <summary>
    /// 特殊事件
    /// </summary>
    public class SpecialEvent
    {
        public string Type { get; set; } = string.Empty;
        public string Name { get; set; } = string.Empty;
        public int DurationMs { get; set; }
        public Dictionary<string, object>? Parameters { get; set; }
    }

    /// <summary>
    /// 动态平衡结果
    /// </summary>
    public class BalanceResult
    {
        public bool Applied { get; set; }
        public List<PlayerBalanceAdjustment> Adjustments { get; set; } = new();
    }

    /// <summary>
    /// 玩家平衡调整
    /// </summary>
    public class PlayerBalanceAdjustment
    {
        public Guid PlayerId { get; set; }
        public string AdjustmentType { get; set; } = string.Empty;
        public double Value { get; set; }
    }
}